import { useReducer, useEffect, useState } from 'react';
import { obj_int, for_obj, GateWay, obj_str, is_empty } from 'sui';
import { g_stepnum, s_stepnum, clear_step, s_state } from 'db/state';

const reducer = (status, action) => {
    const ret = { ...status };
    for_obj(action, (k, v) => ret[k] = v);

    return ret;
};

const useStep = (line, wsn) => {
    const [info, setInfo] = useState();
    const [esop, setEsop] = useState();
    const [para, setPara] = useState();
    const [open_box, openBox] = useState(false);
    const [open_box_st, openBoxSt] = useState(false);
    const [open_box_good, openBoxGood] = useState(false);
    const [open_box_tp, openBoxTp] = useState(false);
    const [open_store, openStore] = useState(false);
    const [open_hand, openHand] = useState(false);

    useEffect(() => {
        clear_step();
    }, [line, wsn]);

    const [status, dispatch] = useReducer(reducer, {});

    const getImg = () => {
        const stepnum = g_stepnum();
        if (!stepnum) {
            return null;
        }

        const path = `${GateWay.url_file()}/${line}/tech/normal/tech`;

        const cs = curStep(stepnum - 1);
        const pic = obj_str(cs, "pic");
        if (!pic) {
            return null;
        }

        return `${path}/${pic}.png`;
    };

    const curStep = (num) => {
        if (num < 0 || is_empty(esop)) {
            return null;
        }

        return esop[num];
    };

    const stepLength = () => {
        if (is_empty(esop)) {
            return 0;
        }

        return esop.length;
    };

    const hset = (k, v) => {
        const d = {};
        d[k] = v;

        dispatch(d);
    };

    const set_stepnum = (n) => {
        const num = parseInt(n, 10);
        s_stepnum(num);

        const tmp = obj_int(status, "stepnum");
        if (tmp !== num) {
            hset("stepnum", num);
        }
    };

    const set_st = (st) => {
        s_state(st);

        const tmp = obj_str(status, "state");
        if (tmp !== st) {
            hset("state", st);
        }
    };

    const set_st_num = (st, n) => {
        set_stepnum(n);
        set_st(st);
    };

    const hget = (k) => {
        if (k === "stepnum") {
            return obj_int(status, k);
        }

        return obj_str(status, k);
    };

    const hclear = () => {
        const ret = { ...status };

        for_obj(ret, (k, v) => ret[k] = "");

        dispatch(ret);

        set_st_num("ready", 0);
        setInfo(null);
        setPara(null);
    };

    return {
        getImg: getImg,
        curStep: curStep,
        stepLength: stepLength,
        setEsop: setEsop,

        para: para,
        setPara: setPara,

        info: info,
        setInfo: setInfo,

        get: hget,
        mset: v => dispatch(v),
        set: hset,
        clear: hclear,

        setErr: v => hset("err", v),

        set_stepnum: set_stepnum,
        set_st_num: set_st_num,
        set_st: set_st,

        open_box: open_box,
        openBox: openBox,

        open_box_st: open_box_st,
        openBoxSt: openBoxSt,

        open_box_good: open_box_good,
        openBoxGood: openBoxGood,

        open_box_tp: open_box_tp,
        openBoxTp: openBoxTp,

        open_store: open_store,
        openStore: openStore,

        open_hand: open_hand,
        openHand: openHand,
    };
};

export {useStep};
